package com.zbf.task.taskweb;

import com.alibaba.fastjson.JSON;
import com.zbf.utils.TwitterIdWorker;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 作者: LCG
 * 日期: 2019/8/12 15:57
 * 描述: 一个测试定时任务的项目
 */

//@RestController
public class TestQuartzController {

    @Autowired
    private TwitterIdWorker twitterIdWorker;

    //动态的添加一个定时任务
    @RequestMapping("taskAdd")
    public void tastAddTask() throws SchedulerException {

        Scheduler defaultScheduler = StdSchedulerFactory.getDefaultScheduler();

        defaultScheduler.start();

        JobDetail jobDetail=JobBuilder.newJob(HelloJob.class)
                .withIdentity("myJobName","myJobGroup")
                .withIdentity(new JobKey("AD1"))
                .build();

        Trigger trigger=TriggerBuilder
                .newTrigger()
                .withIdentity("triggerName","triggerGroup")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .startAt(new Date(new Date().getTime()+10000))
                .endAt(new Date(new Date().getTime()+100000))
                .build();

        defaultScheduler.scheduleJob(jobDetail,trigger);

    }

    /**
     *
     */
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @RequestMapping("taskAdd2/{key}")
    public void testTask02(@PathVariable("key")  String key) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        JobDetail jobDetail=null;
        if(key.equals("01")){
            jobDetail=JobBuilder.newJob(HelloJob.class)
                    .withIdentity(key,"myJobGroup")
                    .build();
            Trigger trigger=TriggerBuilder
                    .newTrigger()
                    .withIdentity("triggerName","triggerGroup")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?"))
                    .startAt(new Date(new Date().getTime()+1000))
                    .endAt(new Date(new Date().getTime()+30000))
                    .build();

            scheduler.scheduleJob(jobDetail,trigger);

            scheduler.start();

        }else{
            jobDetail=JobBuilder.newJob(HelloJob2.class)
                    .withIdentity(key,"myJobGroup02")
                    .build();

            Trigger trigger=TriggerBuilder
                    .newTrigger()
                    .withIdentity("triggerName02","triggerGroup02")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?"))
                    .startAt(new Date(new Date().getTime()+1000))
                    .endAt(new Date(new Date().getTime()+30000))
                    .build();

            scheduler.scheduleJob(jobDetail,trigger);

            scheduler.start();

        }
        
        
    }


    //动态的暂停一个定时任务
    @RequestMapping("zanTing")
    public void toZanTingJob() throws SchedulerException {

        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        scheduler.pauseJob(new JobKey("AD1"));



    }

    //动态的回复一个定时任务
    @RequestMapping("huifu")
    public void toQiDongForZanting() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        scheduler.resumeJob(new JobKey("AD1"));

    }

    //动态的删除一个定时任务
    @RequestMapping("shanchu")
    public void toQshanchu() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        scheduler.deleteJob(new JobKey("AD1"));


    }

    //获取所有正在运行的定时任务
    @RequestMapping("get")
    public void getAllJob() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        //获取所有运行的TAsk
        List<JobExecutionContext> currentlyExecutingJobs = scheduler.getCurrentlyExecutingJobs();

        currentlyExecutingJobs.stream().forEach((jobExecutionContext)->{
            JobDetail jobDetail = jobExecutionContext.getJobDetail();
            JobDataMap jobDataMap = jobDetail.getJobDataMap();

            System.out.println(JSON.toJSONString(jobDataMap));

        });
    }


    //开启一个新的定时任务
    @RequestMapping("startJob/{key}")
    public void toStartJob(@PathVariable("key") String key) throws SchedulerException {

        JobDetail jobDetail = schedulerFactoryBean.getScheduler().getJobDetail(new JobKey(key));

        CronTrigger build = TriggerBuilder
                .newTrigger()
                .withIdentity("triggerName02", "triggerGroup")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?"))
                .startAt(new Date(new Date().getTime() + 1000))
                .endAt(new Date(new Date().getTime() + 30000))
                .build();

        schedulerFactoryBean.getScheduler().scheduleJob(jobDetail, build);

        schedulerFactoryBean.start();

    }
    
    
}
